﻿using ArcGIS.Core.Data;
using ArcGIS.Core.Geometry;
using ArcGIS.Desktop.Core;
using ArcGIS.Desktop.Framework.Threading.Tasks;
using ArcGIS.Desktop.Mapping;
using CCTool.Scripts.Manager;
using CCTool.Scripts.ToolManagers;
using CCTool.Scripts.ToolManagers.Extensions;
using CCTool.Scripts.ToolManagers.Managers;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Security.Cryptography;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using Polygon = ArcGIS.Core.Geometry.Polygon;

namespace CCTool.Scripts.DataPross.FeatureCheck
{
    /// <summary>
    /// Interaction logic for HandleHole.xaml
    /// </summary>
    public partial class HandleHole : ArcGIS.Desktop.Framework.Controls.ProWindow
    {
        // 工具设置标签
        readonly string toolSet = "HandleHole";

        public HandleHole()
        {
            InitializeComponent();

            // 初始化参数选项
            txt_miniArea.Text = BaseTool.ReadValueFromReg(toolSet, "miniArea", "100");
            cb_miniArea.IsChecked = BaseTool.ReadValueFromReg(toolSet, "checkMiniArea", "false").ToBool();
        }

        // 定义一个进度框
        private ProcessWindow processwindow = null;
        string tool_name = "消除内空洞";

        private async void btn_go_Click(object sender, RoutedEventArgs e)
        {
            try
            {
                // 获取指标
                string layerName = combox_fc.ComboxText();
                double miniArea = txt_miniArea.Text.ToDouble();
                bool checkMiniArea = (bool)cb_miniArea.IsChecked;

                string gdbPath = Project.Current.DefaultGeodatabasePath;

                // 判断参数是否选择完全
                if (layerName == "")
                {
                    MessageBox.Show("有必选参数未填写！");
                    return;
                }
                if (miniArea <= 0 && checkMiniArea == true)
                {
                    MessageBox.Show("如果勾选按最小面积查找，请填写大于0的数字");
                    return;
                }

                // 写入本地
                BaseTool.WriteValueToReg(toolSet, "miniArea", miniArea);
                BaseTool.WriteValueToReg(toolSet, "checkMiniArea", checkMiniArea);

                // 打开进度框
                ProcessWindow pw = UITool.OpenProcessWindow(processwindow, tool_name);
                pw.AddMessageTitle(tool_name);

                Close();

                await QueuedTask.Run(() =>
                {
                    string message = checkMiniArea ? $"消除符合条件的图斑：小于{miniArea}平方米的内空洞" : "消除符合条件的图斑：所有内空洞";
                    pw.AddMessageMiddle(20, message);

                    string outputPath = $@"{gdbPath}\{layerName}_消除内空洞";
                    double area = checkMiniArea ? miniArea : 10000000000;

                    Arcpy.EliminatePolygonPart(layerName, outputPath, area, "CONTAINED_ONLY", true);
                });

                pw.AddMessageEnd();
            }
            catch (Exception ee)
            {
                MessageBox.Show(ee.Message + ee.StackTrace);
                return;
            }
        }

        private void btn_help_Click(object sender, RoutedEventArgs e)
        {
            string url = "https://blog.csdn.net/xcc34452366/article/details/148829217";
            UITool.Link2Web(url);
        }

        private void combox_fc_DropDown(object sender, EventArgs e)
        {
            UITool.AddFeatureLayersToComboxPlus(combox_fc, "PP");
        }
    }
}
